import 'package:flutter/material.dart';

void main() => runApp(ItemClickApp());

class ItemClickApp extends StatelessWidget {
  final modelList = [
    Model('CineArts at the Empire', '85 W Portal Ave'),
    Model('The Castro Theater', '429 Castro St'),
    Model('Alamo Drafthouse Cinema', '2550 Mission St'),
    Model('Roxie Theater', '3117 16th St'),
    Model('United Artists Stonestown Twin', '501 Buckingham Way'),
    Model('AMC Metreon 16', '135 4th St #3000'),
    Model('K\'s Kitchen', '1923 Ocean Ave'),
    Model('2Chaiya Thai Restaurant', '72 Claremont Blvd'),
    Model('2La Ciccia', '291 30th St'),
    Model('2CineArts at the Empire', '85 W Portal Ave'),
    Model('2The Castro Theater', '429 Castro St'),
    Model('2Alamo Drafthouse Cinema', '2550 Mission St'),
    Model('2Roxie Theater', '3117 16th St'),
    Model('2United Artists Stonestown Twin', '501 Buckingham Way'),
    Model('2AMC Metreon 16', '135 4th St #3000'),
    Model('2K\'s Kitchen', '1923 Ocean Ave'),
    Model('2Chaiya Thai Restaurant', '72 Claremont Blvd'),
    Model('2La Ciccia', '291 30th St'),
    Model('3Chaiya Thai Restaurant', '72 Claremont Blvd'),
    Model('3La Ciccia', '291 30th St'),
    Model('3CineArts at the Empire', '85 W Portal Ave'),
    Model('3The Castro Theater', '429 Castro St'),
    Model('3Alamo Drafthouse Cinema', '2550 Mission St'),
    Model('3Roxie Theater', '3117 16th St'),
    Model('3United Artists Stonestown Twin', '501 Buckingham Way'),
    Model('3AMC Metreon 16', '135 4th St #3000'),
    Model('3K\'s Kitchen', '1923 Ocean Ave'),
    Model('3Chaiya Thai Restaurant', '72 Claremont Blvd'),
    Model('3La Ciccia', '291 30th St'),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: ClickItemListView(
          modelList, (position) => debugPrint("${modelList[position]}")),
    ));
  }
}

class ClickItemListView extends StatelessWidget {
  final List<Model> modelList;
  final OnItemClickListener listener;

  ClickItemListView(this.modelList, this.listener);

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemBuilder: (context, index) =>
          ItemView(modelList[index], index, listener),
      itemCount: modelList.length,
      separatorBuilder: (_, index) => Divider(
        color: Colors.grey,
      ),
    );
  }
}

class ItemView extends StatelessWidget {
  final Model model;
  final int position;
  final OnItemClickListener listener;

  ItemView(this.model, this.position, this.listener);

  @override
  Widget build(BuildContext context) {
    final row = Row(
      children: [
        Container(
          margin: EdgeInsets.all(18.0),
          child: Icon(
            Icons.adb,
            color: Colors.red,
          ),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              model.msg,
              style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w400),
            ),
            Text(model.desc),
          ],
        )
      ],
    );

    return InkWell(
      child: row,
      onTap: () => listener(position),
    );
  }
}

// Item 回调函数
typedef OnItemClickListener = void Function(int positon);

class Model {
  final String msg;
  final String desc;

  Model(this.msg, this.desc);

  @override
  String toString() {
    return 'Model{msg: $msg, desc: $desc}';
  }
}
